var express = require('express');
var router = express.Router();
const classifySchema = require('../module/classify');
const shopslistSchema = require('../module/shopslist');
const shopsdetailSchema = require('../module/shopsdetail');
const speciallistSchema = require('../module/speciallist');
const swiperSchema = require('../module/swiper');


// 获得所有类别的具体分类
router.get('/classify', function (req, res, next) {
  if (req.query.id) {
    let { id } = req.query
    classifySchema.find({ id }, (err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        msg: '请求成功',
        data: doc
      })
    })
  } else {
    classifySchema.find({}, (err, doc) => {
      if (err) throw err;
      res.json({
        status: 0,
        msg: '请求成功',
        data: doc
      })
    })
  }

});


// 各分类对应的商品列表,获取前六条
router.post('/shoplist', function (req, res, next) {
  let { id, index } = req.body
  shopslistSchema.find({ id: id }, { result: { $slice: [+index * 6, 6] } }, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      msg: '请求成功',
      data: doc
    })
  })
});


// 点击商品商品列表获取的商品详情
router.post('/detail', function (req, res, next) {
  let { id } = req.body
  shopsdetailSchema.find({ id }, (err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      msg: '请求成功',
      data: doc
    })
  })
});


// 获得专题的数据
router.post('/speciallistmore', function (req, res, next) {
  let { index } = req.body
  speciallistSchema.find({}).skip(+index * 6).limit(6).exec((err, doc) => {
    if (err) throw err;
    res.json({
      status: 0,
      msg: '请求成功',
      data: doc
    })
  })
});

//小轮播图接口
router.get('/smallswiper',function(req,res,next){
  swiperSchema.find({},(err,doc)=>{
    res.json({
      status:0,
      msg:"获取成功",
      data:doc
    })
  })
})



//测试用
// router.get('/add', function (req, res, next) {
//   // let {id}=req.query
//   shopsdetailSchema.create({id:"112233"}, (err, doc) => {
//     if (err) throw err;
//     res.json({
//       status: 0,
//       msg: '请求成功',
//       data: doc
//     })
//   })
// });

module.exports = router;